//
//  FZEmotionViewModel.swift
//  MyWeiBo
//
//  Created by fangzhengwei on 2017/4/7.
//  Copyright © 2017年 fangzhengwei. All rights reserved.
//

import UIKit

let recentEmotionArrPath = (NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true).last! as NSString).appendingPathComponent("recentEmotionArrPath.data")
class FZEmotionViewModel: NSObject {

    static let sharedEmotion : FZEmotionViewModel = FZEmotionViewModel()
 
    fileprivate override init() {
        super.init()
      print(defaultArr.count)
      print(emojiArr.count)
      print(LXHArr.count)
    }
    //获取bundle路径
    
     lazy var Allbundle : Bundle = {
       
      let bundlepath = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil)!
     
     let bundle = Bundle(path: bundlepath)!
      
      return bundle

    }()
    
    //默认表情
    fileprivate lazy var defaultArr : [FZEmotionModel] = {
    
      let array = self.loadEmotionDataArray(folderName: "default", fileName: "info.plist")
      
        
      return array
    }()
    
    //emoji表情
    
    fileprivate lazy var emojiArr : [FZEmotionModel] = {
    
        let array = self.loadEmotionDataArray(folderName: "emoji", fileName: "info.plist")
        
        return array
    
    }()
    
    //浪小花
    
    fileprivate lazy var LXHArr : [FZEmotionModel] = {
        
        return self.loadEmotionDataArray(folderName: "lxh", fileName: "info.plist")
    }()
    //最近表情
    fileprivate lazy var recentArr : [FZEmotionModel] = {
        
        if let recentData = self.loadRecentEmotionData() {
            return recentData
            
        }else{
            let array = [FZEmotionModel]()
            return array
        }
    }()
    //计算每种表情分成多少页显示
    //获取总的三维数组数据
       lazy var allEmotionArray : [[[FZEmotionModel]]]? = {
       
       return [
        
          [self.recentArr],
          self.CalculationEmotionPage(array: self.defaultArr),
          self.CalculationEmotionPage(array: self.emojiArr),
          self.CalculationEmotionPage(array: self.LXHArr)
        ]
        
    }()
    
    
    //将表情分成页
    func CalculationEmotionPage(array : [FZEmotionModel]) -> [[FZEmotionModel]] {
        //多少页
        let emotionPage = (array.count - 1) / emotionOfPage + 1
        
        var tempArray : [[FZEmotionModel]] = [[FZEmotionModel]]()
        
        for i in 0 ..< emotionPage {
            //开始截取位置
            let loc = i * emotionOfPage
            //截取的长度
            var len = emotionOfPage
            
            if loc + len > array.count {
                len = array.count - loc
            }
            
            let interceptRange = NSRange(location: loc, length: len)
            
            let subArray = (array as NSArray).subarray(with: interceptRange) as! [FZEmotionModel]
            
            tempArray.append(subArray)
        }
        return tempArray
    }
    
    
    

//   获取表情数据
    func loadEmotionDataArray(folderName : String,fileName : String) -> [FZEmotionModel] {
        
        let MosaicFilePath = folderName + "/" + fileName
        
        let path = self.Allbundle.path(forResource: MosaicFilePath, ofType: nil)!
        
        let array = NSArray(contentsOfFile: path)!
        
        let data = NSArray.yy_modelArray(with: FZEmotionModel.self, json: array) as! [FZEmotionModel]
        
        for model in data {
            if model.type == "0" {
                //  图片表情全路径
                let path = Allbundle.path(forResource: folderName, ofType: nil)! + "/" + model.png!
                //  设置图片的全路径
                model.allPath = path
                model.folderName = folderName
            }
        }

        return data
    
    }
    
    func saveRecentEmotionArray(emotion : FZEmotionModel) {
        
        for (i , currentEmotion) in recentArr.enumerated() {
            
            if emotion.type == "0" {
            
            if currentEmotion.chs == emotion.chs {
                
                recentArr.remove(at: i)
                break
               }
                
            }else{
                
                if currentEmotion.code == emotion.code {
                    recentArr.remove(at: i)
                    break
                }
            }
            
        }
        
        recentArr.insert(emotion, at: 0)
        
        if recentArr.count > 20 {
            recentArr.removeLast()
        }
        
        allEmotionArray?[0][0] = recentArr
        
        NSKeyedArchiver.archiveRootObject(recentArr, toFile: recentEmotionArrPath)
    }
    
    func loadRecentEmotionData()-> [FZEmotionModel]? {
        
        let localArray = NSKeyedUnarchiver.unarchiveObject(withFile: recentEmotionArrPath) as? [FZEmotionModel]
        return localArray

    }
    
    //根据表情描述获取表情模型
   func selectedEmotion(chs : String) -> FZEmotionModel? {
        
        for emotion in defaultArr {
            if emotion.chs == chs {
                return emotion
            }
            
        }
        for emotion in LXHArr {
            if emotion.chs == chs {
                return emotion
            }
            
        }
        return nil
    }
}



























